/*
 * Copyright 2017-2101 Innel.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hykj.xxltest.service;

import com.hykj.xxltest.base.AppBack;
import com.hykj.xxltest.base.BaseService;
import com.hykj.xxltest.base.MsgException;
import com.hykj.xxltest.mapper.PayOrderMapper;
import com.hykj.xxltest.model.PayOrderModel;
import com.hykj.xxltest.utils.AlipayUtil;
import com.hykj.xxltest.utils.TokenUtil;
import com.hykj.xxltest.utils.WechatpayNotifyVerifyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付订单凭据
 *
 * @author innel
 * @email 2638086622@qq.com
 * @date 2018-04-05 13:13:09
 */
@Service
public class PayOrderService extends BaseService<PayOrderModel, PayOrderMapper> {


    @Autowired
    UserService userService;



    /**
     * 生成支付凭证
     *
     * @param payType   支付类型 1支付宝 2微信 3余额
     * @param price
     * @param targetId
     * @param orderType
     * @param userId
     * @return
     */
    public AppBack payOrder(int payType, BigDecimal price, int targetId, int orderType, int userId, HttpServletRequest request) {

        PayOrderModel payOrderModel = new PayOrderModel();
        payOrderModel.setPayType(payType);
        payOrderModel.setPrice(price);
        payOrderModel.setStatus(0);
        payOrderModel.setTargetId(targetId);
        payOrderModel.setOrderType(orderType);
        payOrderModel.setOrderCode(TokenUtil.getInstance().code("PO"));
        payOrderModel.setUserId(userId);
        create(payOrderModel);

        switch (payType) {
            case 1:
                String res = AlipayUtil.alipay(payOrderModel.getOrderCode(), price);
                return new AppBack().add("aliPay" , res);
            case 2:
                String prepayId = WechatpayNotifyVerifyUtil.getPayReq(payOrderModel.getOrderCode(), price, request.getRemoteAddr());
                Map<String, String> wxPay = WechatpayNotifyVerifyUtil.pay(prepayId);
                return new AppBack().add("wxPay" , wxPay);
            default:
                throw new MsgException("支付类型错误");
        }
    }

    public void payOrderSuccess(String code) {
        Map params = new HashMap();
        params.put("orderCode" , code);
        PayOrderModel payOrderModel = getListLite(params).get(0);
        //处理重复调用
        if (payOrderModel.getStatus() != 0) {
            return;
        }
        payOrderModel.setStatus(1);
        payOrderModel.setPayTime(new Date());
        update(payOrderModel);

        switch (payOrderModel.getOrderType()) {
//            case 1://购买课程
//                userCourseService.joinCourse(payOrderModel.getUserId(),payOrderModel.getTargetId());
//                break;
//            case 2:
//                orderService.buySucucess(payOrderModel.getTargetId());
//                break;
//            case 3://购买会员卡
//                userCardService.buyCardSuccess(payOrderModel.getTargetId());
//                break;
            default:
        }
    }


}